package com.im.controller;

import com.im.entity.dto.UserLoginDTO;
import com.im.entity.dto.UserRegisterDTO;
import com.im.entity.dto.UpdatePasswordDTO;
import com.im.entity.dto.UpdateUserInfoDTO;
import com.im.entity.vo.UserInfoVO;
import com.im.entity.ImUser;
import com.im.service.IImUserService;
import com.im.utils.UserContext;
import com.im.web.response.Result;
import com.im.utils.TokenUtil;
import com.im.web.response.ReturnCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * <p> 用户模块前端控制器 </p>
 * @author im
 * @since 2025-01-30
 */
@RestController
@RequestMapping("/link-IM/im-user")
@Tag(name = "用户模块")
public class ImUserController {

    @Autowired
    private IImUserService imUserService;

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<UserInfoVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        UserInfoVO userInfoVO = imUserService.login(userLoginDTO);
        return Result.success(userInfoVO);
    }

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result<UserInfoVO> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        UserInfoVO userInfoVO = imUserService.register(userRegisterDTO);
        return Result.success(userInfoVO);
    }

    /**
     * 用户退出（让 Token 失效）
     */
    @Operation(summary = "用户退出")
    @PostMapping("/logout")
    public Result<String> logout(@RequestHeader("Authorization") String token) {
        Integer userId = tokenUtil.getUserIdFromToken(token);
        if (userId != null) {
            tokenUtil.invalidateToken(userId);
            return Result.success("退出成功");
        }
        return Result.fail(ReturnCode.INTERNAL_SERVER_ERROR,"无效 Token");
    }

    /**
     * 用户信息查询
     */
    @Operation(summary = "其他用户信息查询")
    @GetMapping("/info")
    public Result<UserInfoVO> getUserInfo(Integer userId) {
        UserInfoVO userInfo = imUserService.getUserById(userId);
        return Result.success(userInfo);
    }
    /**
     * 当前用户信息查询
     */
    @Operation(summary = "当前用户信息查询")
    @GetMapping("/current/info")
    public Result<UserInfoVO> getCurrentUserInfo() {
        return Result.success(UserContext.getUser());
    }



    /**
     * **修改用户信息**
     */
    @Operation(summary = "用户信息修改（nickname）")
    @PostMapping("/update")
    public Result<UserInfoVO> updateUserInfo(@RequestParam("nickname") String nickname) {
        UserInfoVO updatedUser = imUserService.updateUsername(UserContext.getUser().getId(), nickname);
        return Result.success(updatedUser);
    }

    /**
     * **修改用户密码**
     */
    @Operation(summary = "当前用户密码修改")
    @PostMapping("/update/password")
    public Result<String> updatePassword(@Valid @RequestBody UpdatePasswordDTO passwordDTO) {
        imUserService.updatePassword(UserContext.getUser().getId(), passwordDTO);
        return Result.success("密码修改成功");
    }

    /**
     * **上传并更新用户头像**
     */
    @Operation(summary = "当前用户头像修改")
    @PostMapping("/update-avatar")
    public Result<String> updateAvatar(@RequestParam("file") MultipartFile file) {
        String avatarUrl = imUserService.updateAvatar(UserContext.getUser().getId(),file);
        return Result.success(avatarUrl);
    }
}
